package com.futvan.z.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.futvan.z.framework.common.bean.Code;
import com.futvan.z.framework.common.bean.Result;
import com.futvan.z.framework.common.bean.z_form_table;
import com.futvan.z.framework.common.service.CommonService;
import com.futvan.z.framework.core.SuperAction;
import com.futvan.z.framework.core.z;
import com.futvan.z.framework.util.CommonServicesUtil;
import com.futvan.z.framework.util.MathUtil;
import com.futvan.z.framework.util.StringUtil;
import com.futvan.z.system.cms_column.cms_column;
import com.futvan.z.system.cms_info.cms_info;
import com.futvan.z.system.cms_info.cms_info_detail;
import com.futvan.z.system.zproduct.z_product;
@Controller
public class WebAction extends SuperAction {
	@Autowired
	private WebService webService;
	@Autowired
	private CommonService commonService;
	private int rowcount_int = 5;//分页显示行数
	private List<cms_column> detailColumnList = new ArrayList<cms_column>();//下级栏目集合

	@RequestMapping(value="/index")
	public ModelAndView index(String cityId) throws Exception {
		ModelAndView  model = new ModelAndView("web/index");
		//获取所有一级菜单
		model.addObject("L0List", z.cmsColumn0List);
		//获取所有栏目的子栏目
		model.addObject("cmsColumnDetail", z.cmsColumnDetail);

		//当前一级栏目
		model.addObject("nowColumnID", getNowColumnID(""));

		//获取1组bunner列表
		model.addObject("bunnerList", z.cmsBunnerList.get("1"));
		return model;
	}

	/**
	 * 获取一级栏目ID
	 * @param zid
	 * @return
	 */
	private String getNowColumnID(String zid) {
		String colId = "";
		if(z.isNotNull(zid)) {
			cms_column col = z.cmsColumn.get(zid);
			if(z.isNull(col)) {
				//zid是新闻ID
				col = z.cmsColumn.get(z.cms.get(zid).getColumnid());
			}
			while (true) {
				String pid = col.getParentid();
				//获取上级栏目
				if(z.isNotNull(pid)) {
					col = z.cmsColumn.get(pid);
				}else {
					colId = col.getZid();
					break;
				}
			}
		}else {
			colId = "index";
		}
		return colId;
	}

	/**
	 * 查看CMS
	 * @param zid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/open_cms")
	public ModelAndView open_cms(String zid) throws Exception {
		//判断zid转的是不是number
		if(z.isNotNull(z.cmsForNumber.get(zid))) {
			zid = z.cmsForNumber.get(zid).getZid();
		}

		ModelAndView  model = new ModelAndView("web/cms/open_cms");
		//获取所有一级菜单
		model.addObject("L0List", z.cmsColumn0List);
		//获取所有栏目的子栏目
		model.addObject("cmsColumnDetail", z.cmsColumnDetail);
		//获取新闻信息
		cms_info c = sqlSession.selectOne("cms_info_select_zid", zid);
		cms_info_detail cdq = new cms_info_detail();
		cdq.setPid(c.getZid());
		cdq.setSql_order_by(" seq ");
		List<cms_info_detail> cdlist = sqlSession.selectList("cms_info_detail_select",cdq );
		c.setCms_info_detail_list(cdlist);
		model.addObject("cms",c);

		//获取热门新闻列表
		model.addObject("hot_cms_list", getHotCmsInfoList());
		//获取2组bunner列表
		model.addObject("bunnerList2", z.cmsBunnerList.get("2"));

		//生成内页导航
		model.addObject("tn", getTapNavigation(zid));

		//当前一级栏目
		model.addObject("nowColumnID", getNowColumnID(zid));
		return model;
	}

	@RequestMapping(value="/cms_column0")
	public ModelAndView cms_column0(String zid) throws Exception {
		ModelAndView  model = new ModelAndView("web/cms/cms_column0");
		//获取所有一级菜单
		model.addObject("L0List", z.cmsColumn0List);
		//获取所有栏目的子栏目
		model.addObject("cmsColumnDetail", z.cmsColumnDetail);
		//获取子栏目
		detailColumnList = new ArrayList<cms_column>();
		getDetailColumnList(zid);
		detailColumnList.add(z.cmsColumn.get(zid));
		model.addObject("detailList", detailColumnList);
		//当前一级栏目
		model.addObject("nowColumnID", getNowColumnID(zid));
		return model;
	}

	@RequestMapping(value="/cms_column2")
	public ModelAndView cms_column2(String zid) throws Exception {
		ModelAndView  model = new ModelAndView("web/cms/cms_column2");
		//获取所有一级菜单
		model.addObject("L0List", z.cmsColumn0List);
		//获取所有栏目的子栏目
		model.addObject("cmsColumnDetail", z.cmsColumnDetail);
		//获取子栏目
		detailColumnList = new ArrayList<cms_column>();
		getDetailColumnList(zid);
		detailColumnList.add(z.cmsColumn.get(zid));
		model.addObject("detailList", detailColumnList);
		//当前一级栏目
		model.addObject("nowColumnID", getNowColumnID(zid));
		return model;
	}

	/**
	 * 相册展示
	 * @param zid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/cms_column3")
	public ModelAndView cms_column3(String zid) throws Exception {
		ModelAndView  model = new ModelAndView("web/cms/cms_column3");
		//获取所有一级菜单
		model.addObject("L0List", z.cmsColumn0List);
		//获取所有栏目的子栏目
		model.addObject("cmsColumnDetail", z.cmsColumnDetail);
		//获取当前栏目对像
		model.addObject("nowColumn", z.cmsColumn.get(zid));
		//获取子栏目
		detailColumnList = new ArrayList<cms_column>();
		getDetailColumnList(zid);
		detailColumnList.add(z.cmsColumn.get(zid));
		model.addObject("detailList", detailColumnList);
		//当前一级栏目
		model.addObject("nowColumnID", getNowColumnID(zid));
		return model;
	}

	@RequestMapping(value="/cms_column1")
	public ModelAndView cms_column1(String zid,String pagenum) throws Exception {
		if(z.isNull(pagenum)) {
			pagenum = "1";
		}
		ModelAndView  model = new ModelAndView("web/cms/cms_column1");
		//获取所有一级菜单
		model.addObject("L0List", z.cmsColumn0List);
		//获取所有栏目的子栏目
		model.addObject("cmsColumnDetail", z.cmsColumnDetail);
		//获取当前栏目对像
		model.addObject("nowColumn", z.cmsColumn.get(zid));
		//获取子栏目
		detailColumnList = new ArrayList<cms_column>();
		getDetailColumnList(zid);
		detailColumnList.add(z.cmsColumn.get(zid));
		model.addObject("detailList", detailColumnList);

		//根据子栏目获取资讯信息
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT * FROM cms_info c where ");
		String dCid = getDetailListid(detailColumnList);
		sql.append("c.columnid IN("+dCid+") ");
		sql.append("and c.is_publish = '1' ");
		sql.append("order by c.istop desc,c.publish_time desc");
		//添加分页

		if(z.isNotNull(pagenum)) {
			int pagenum_int = Integer.valueOf(pagenum);//页号

			if(pagenum_int>0 && rowcount_int>0) {//页号和分布显示行数都必须大于0
				int row_num = (pagenum_int-1)*rowcount_int;//分页开始行号
				sql.append(" limit "+row_num+","+rowcount_int);
			}else {
				sql.append(" limit 0,"+rowcount_int);
			}
		}
		List<cms_info> clist = sqlSession.selectList("cms_info_select_sql", sql);
		model.addObject("clist", clist);
		if(clist.size()>0) {
			//获取总页数
			StringBuffer sql_pagecount = new StringBuffer();
			sql_pagecount.append("SELECT count(*) FROM cms_info c where ");
			sql_pagecount.append("c.columnid IN("+dCid+") ");
			sql_pagecount.append("and c.is_publish = '1' ");
			int datacount = sqlSession.selectOne("selectoneint", sql_pagecount);
			String pagcount = MathUtil.getPageCount(datacount, rowcount_int);
			model.addObject("PagingHtml", CreatePagingHtml(Integer.parseInt(pagenum),Integer.parseInt(pagcount)));
		}

		model.addObject("zid", zid);

		//获取热门新闻列表
		model.addObject("hot_cms_list", getHotCmsInfoList());
		//获取2组bunner列表
		model.addObject("bunnerList2", z.cmsBunnerList.get("2"));
		//生成内页导航
		model.addObject("tn", getTapNavigation(zid));
		//当前一级栏目
		model.addObject("nowColumnID", getNowColumnID(zid));
		return model;
	}


	@RequestMapping(value="/cms_search")
	public ModelAndView cms_search(String sinfo) throws Exception {
		if(z.isNull(sinfo)) {
			sinfo = "";
		}
		ModelAndView  model = new ModelAndView("web/cms/cms_search");
		//获取所有一级菜单
		model.addObject("L0List", z.cmsColumn0List);
		//获取所有栏目的子栏目
		model.addObject("cmsColumnDetail", z.cmsColumnDetail);

		//根据子栏目获取资讯信息
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT * FROM cms_info c where ");
		sql.append("c.is_publish = '1' ");
		sql.append("and ( c.title1 like '%"+sinfo+"%' || c.title2 like '%"+sinfo+"%'  || c.title3 like '%"+sinfo+"%' || c.info like '%"+sinfo+"%') ");
		sql.append("order by c.istop desc,c.publish_time desc");
		List<cms_info> clist = sqlSession.selectList("cms_info_select_sql", sql);
		model.addObject("clist", clist);

		//获取热门新闻列表
		model.addObject("hot_cms_list", getHotCmsInfoList());
		//获取2组bunner列表
		model.addObject("bunnerList2", z.cmsBunnerList.get("2"));
		model.addObject("sinfo", sinfo);

		//当前一级栏目
		model.addObject("nowColumnID", getNowColumnID(""));
		return model;
	}

	/**
	 * 点赞
	 * @param zid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/cmslike")
	public ModelAndView cmslike(String zid) throws Exception {
		ModelAndView  model = new ModelAndView("web/cms/open_cms");
		//获取所有一级菜单
		model.addObject("L0List", z.cmsColumn0List);
		//获取所有栏目的子栏目
		model.addObject("cmsColumnDetail", z.cmsColumnDetail);
		//点赞
		int num = sqlSession.update("update", "update cms_info set likes_amount = likes_amount+1 where zid = '"+zid+"'");
		//获取新闻信息
		cms_info c = sqlSession.selectOne("cms_info_select_zid", zid);
		List<cms_info_detail> cdlist = sqlSession.selectList("cms_info_detail_select_pid", c.getZid());
		c.setCms_info_detail_list(cdlist);
		model.addObject("cms",c);

		//生成内页导航
		model.addObject("tn", getTapNavigation(zid));
		//当前一级栏目
		model.addObject("nowColumnID", getNowColumnID(zid));
		//获取热门新闻列表
		model.addObject("hot_cms_list", getHotCmsInfoList());
		//获取2组bunner列表
		model.addObject("bunnerList2", z.cmsBunnerList.get("2"));
		return model;
	}

	/**
	 * 获取下级栏目列表
	 */

	private void getDetailColumnList(String zid) {
		List<cms_column> dl = z.cmsColumnDetail.get(zid);
		detailColumnList.addAll(dl);

		for (cms_column col : dl) {
			getDetailColumnList(col.getZid());
		}
	}

	/**
	 * 创建分页
	 * @param rownum
	 * @param page_count
	 * @return
	 */
	private String CreatePagingHtml(int rownum,int page_count){
		StringBuffer out_html = new StringBuffer();
		//页号
		if(rownum==0) {
			rownum = 1;
		}

		int rowcount = rowcount_int;

		//页数
		if(page_count==0) {
			page_count = 1;
		}

		out_html.append("<ul class=\"pagination \">").append("\r\n");
		//上一页按钮,如果页号为1，把上一页只读
		if(rownum<=1){
			out_html.append("<li class=\"page-item disabled\"><a class=\"page-link\" href=\"#\">&nbsp;<i class=\"fa fa-fast-backward\"></i>&nbsp;</a></li>").append("\r\n");
			out_html.append("<li class=\"page-item disabled\"><a class=\"page-link\" href=\"#\">&nbsp;<i class=\"fa fa-backward\"></i>&nbsp;</a></li>").append("\r\n");
		}else{
			out_html.append("<li class=\"page-item \"><a class=\"page-link\" href=\"javascript:void(0);\" onclick=\"cms_form_query(1)\">&nbsp;<i class=\"fa fa-fast-backward\"></i>&nbsp;</a></li>").append("\r\n");
			out_html.append("<li class=\"page-item \"><a class=\"page-link\" href=\"javascript:void(0);\" onclick=\"cms_form_query("+(rownum-1)+")\">&nbsp;<i class=\"fa fa-backward\"></i>&nbsp;</a></li>").append("\r\n");
		}

		//如果总页数大于5
		if(page_count>6){
			//如果页号大于6
			if(rownum>6){
				out_html.append("<li class=\"page-item\"><a class=\"page-link\" href=\"javascript:void(0);\" onclick=\"cms_form_query("+1+")\">1</a></li> ").append("\r\n");
				out_html.append("<li class=\"page-item disabled\"><a class=\"page-link\" href=\"#\">...</a></li> ").append("\r\n");
			}
			if(rownum>6){
				for (int i = rownum-3; i <= rownum+3; i++) {
					if(i==rownum){
						out_html.append("<li class=\"page-item active\"><a class=\"page-link\" href=\"javascript:void(0);\" onclick=\"cms_form_query("+i+")\"  >"+i+"</a></li> ").append("\r\n");
					}else{
						if(i<=page_count){
							out_html.append("<li class=\"page-item\"><a class=\"page-link\" href=\"javascript:void(0);\" onclick=\"cms_form_query("+i+")\">"+i+"</a></li> ").append("\r\n");
						}
					}
				}
			}else{
				for (int i = 1; i <= 7; i++) {
					if(i==rownum){
						out_html.append("<li class=\"page-item active\"><a class=\"page-link\" href=\"javascript:void(0);\" onclick=\"cms_form_query("+i+")\"  >"+i+"</a></li> ").append("\r\n");
					}else{
						out_html.append("<li class=\"page-item\"><a class=\"page-link\" href=\"javascript:void(0);\" onclick=\"cms_form_query("+i+")\">"+i+"</a></li> ").append("\r\n");
					}
				}
			}
			if(page_count>7 && page_count-rownum>3){
				out_html.append("<li class=\"page-item disabled\"><a class=\"page-link\" href=\"#\">...</a></li> ").append("\r\n");
				out_html.append("<li class=\"page-item\"><a class=\"page-link\" href=\"javascript:void(0);\" onclick=\"cms_form_query("+page_count+")\">"+page_count+"</a></li> ").append("\r\n");
			}
		}else{
			//如果总页数小于等于5
			for (int i = 1; i <= page_count; i++) {
				if(i==rownum){
					out_html.append("<li class=\"page-item active\"><a class=\"page-link\" href=\"javascript:void(0);\" onclick=\"cms_form_query("+i+")\"  >"+i+"</a></li> ").append("\r\n");
				}else{
					out_html.append("<li class=\"page-item\"><a class=\"page-link\" href=\"javascript:void(0);\" onclick=\"cms_form_query("+i+")\"  >"+i+"</a></li> ").append("\r\n");
				}

			}
		}

		//下一页按钮
		if(rownum>=page_count){
			out_html.append("<li class=\"page-item disabled\"><a class=\"page-link\" href=\"#\">&nbsp;<i class=\"fa fa-forward\"></i>&nbsp;</a></li>").append("\r\n");
			out_html.append("<li class=\"page-item disabled\"><a class=\"page-link\" href=\"#\">&nbsp;<i class=\"fa fa-fast-forward\"></i>&nbsp;</a></li>").append("\r\n");
		}else{
			out_html.append("<li class=\"page-item\"><a class=\"page-link\" href=\"javascript:void(0);\" onclick=\"cms_form_query("+(rownum+1)+")\"  >&nbsp;<i class=\"fa fa-forward\"></i>&nbsp;</a></li> ").append("\r\n");
			out_html.append("<li class=\"page-item\"><a class=\"page-link\" href=\"javascript:void(0);\" onclick=\"cms_form_query("+page_count+")\"  >&nbsp;<i class=\"fa fa-fast-forward\"></i>&nbsp;</a></li> ").append("\r\n");
		}
		out_html.append("</ul>").append("\r\n");
		return out_html.toString();
	}

	/**
	 * 生成子栏目ID，SQL in语句查询条件
	 * @param detailColumnList
	 * @return
	 */
	private String getDetailListid(List<cms_column> detailColumnList) {
		List<String> lid = new ArrayList<String>();
		for (cms_column c : detailColumnList) {
			lid.add(c.getZid());
		}
		return StringUtil.ListToString(lid, ",", "'", "'");
	}

	/**
	 * 获取热门新闻取10条
	 * @return
	 */
	private List<cms_info> getHotCmsInfoList(){
		List<cms_info> list = new ArrayList<cms_info>();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT * FROM cms_info c ");
		sql.append("WHERE c.is_publish = '1' ");
		sql.append("ORDER BY c.istop DESC,c.reading_amount DESC ");
		sql.append("LIMIT 0, 9 ");
		list = sqlSession.selectList("cms_info_select_sql", sql);
		return list;
	}

	/**
	 * 生成内页导航
	 * @param zid 栏目ID
	 * @return
	 */
	private String getTapNavigation(String zid) {
		String html = "";
		if(z.isNotNull(zid)) {
			//判读当前zid是否是栏目ID
			cms_column col = z.cmsColumn.get(zid);
			if(z.isNull(col)) {
				//当如当前ID不是栏目ID，以CMSid找到栏目ID
				cms_info cms = z.cms.get(zid);
				col = z.cmsColumn.get(cms.getColumnid());
			}

			if(z.isNotNull(col)) {
				tempColList = new ArrayList<cms_column>();
				//获取所有上级
				getSuperColunmAll(col.getZid());
				//遍历所有栏目
				for (int i = 0; i < tempColList.size(); i++) {
					cms_column scol = tempColList.get(i);
					
					//判读是否在新窗口打开
					String target = "";
					if("1".equals(scol.getIsblank())) {
						target = " target=\"_blank\" ";
					}
					
					if(i==0) {
						html = "<a class=\"clearA text-danger\" href=\""+scol.getUrl()+"\" "+target+">"+scol.getColumn_name()+"</a>";
					}else {
						html = "<a class=\"clearA\" href=\""+scol.getUrl()+"\" "+target+">"+scol.getColumn_name()+"</a> > "+html;
					}

				}

			}
		}
		return html;
	}
	private List<cms_column> tempColList;
	private void getSuperColunmAll(String cid) {
		//获取本级
		cms_column col = z.cmsColumn.get(cid);
		tempColList.add(col);
		//判读是否有上级
		if(z.isNotNull(col.getParentid())) {
			getSuperColunmAll(col.getParentid());
		}
	}




	/**
	 * 	查看产品列表
	 * @param zid
	 * @return
	 * @throws Exception
	 * 
	 * 
	 */
	@RequestMapping(value="/product_view_list")
	public ModelAndView product_view_list(@RequestParam HashMap<String,String> bean) throws Exception {
		return commonService.list(bean,"web/product_list",request);
	}

	/**
	 * 	查看产品
	 * @param zid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/product_view")
	public ModelAndView product_view(String zid) throws Exception {
		ModelAndView  model = new ModelAndView("web/product");
		if(z.isNotNull(zid)) {
			z_product zp = sqlSession.selectOne("z_product_select_zid", zid);
			model.addObject("zp", zp);
		}
		return model;
	}



}
